package org.fuys.coder.domain.config.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.common.exception.BaseException;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.infrastructure.dao.config.RecommendConfigMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.config
 * @className: CoderRecommendConfig
 * @author: WangJing
 * @description: 推荐相关配置
 * @date: 2024/6/25 18:01
 * @version: 1.0
 */
@Configuration
@Component("recommendConfig")
@TableName("recommend_config")
public class CoderRecommendConfig extends BaseConfig implements Serializable {
    private Integer categoryTypeNums;
    private Integer substanceNums;
    private Double thumbWeight;
    private Double favWeight;
    private Double unlikeWeight;
    private Integer hotRankNum;
    private Integer hotSelectNum;
    private Integer hotRankDay;
    private Double hotThumbWeight;
    private Double hotFavWeight;
    //推拉流粉丝数量分界点 超过此数量的 拉 否则推
    private Integer pushCount;


    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Resource
    @TableField(exist = false)
    private RecommendConfigMapper recommendConfigMapper;

    @PostConstruct
    public void initConfigValue(){
        final CoderRecommendConfig coderRecommendConfig = recommendConfigMapper.selectOne(new LambdaQueryWrapper<CoderRecommendConfig>()
                .eq(CoderRecommendConfig::getVersion, 1));
        if(coderRecommendConfig==null){
            throw new BaseException("recommend相关配置注入异常:数据库中不存在相关数据");
        }
        this.updateAllValue(1,coderRecommendConfig);
    }

    @Override
    protected void setConfigDBValue(Object instance, Integer version) throws RuntimeException {
        if(instance instanceof CoderRecommendConfig){
            CoderRecommendConfig config=(CoderRecommendConfig) instance;
            config.setVersion(version);
            recommendConfigMapper.updateById(config);
        }else{
            throw new BusinessException("无法同步到数据库 请检查传递的数据格式!-- 来自推荐配置");
        }
    }

    @Override
    protected void switchConfigVersion(Integer version) {
        final CoderRecommendConfig config = recommendConfigMapper.selectOne(new LambdaQueryWrapper<CoderRecommendConfig>()
                .eq(CoderRecommendConfig::getVersion, version));
        if(ObjectUtils.isEmpty(config)){
            throw new BusinessException("没有此版本号的配置文件 请核对之后再次切换 或添加新版本 --来自推荐配置");
        }
        this.updateAllValue(version,config);
    }

    @Override
    protected void addConfigVersion(AppConfigAddVersionReq req) {
        CoderRecommendConfig recommendConfig=new CoderRecommendConfig();
        recommendConfig.setVersion(req.getVersion());
        recommendConfig.setDescription(req.getDesc());
        recommendConfig.setHotFavWeight(req.getHotFavWeight());
        recommendConfig.setHotThumbWeight(req.getHotThumbWeight());
        recommendConfig.setHotRankDay(req.getHotRandDay());
        recommendConfig.setHotSelectNum(req.getHotSelectNum());
        recommendConfig.setHotRankNum(req.getHotRankNum());
        recommendConfig.setPushCount(req.getPushCount());
        recommendConfig.setCategoryTypeNums(req.getCategoryTypeNums());
        recommendConfig.setUnlikeWeight(req.getUnlikeWeight());
        recommendConfig.setSubstanceNums(req.getSubstanceNums());
        this.recommendConfigMapper.insert(recommendConfig);
    }

    @Override
    protected List<? extends BaseConfig> getConfigList() {
        return this.recommendConfigMapper.selectList(new LambdaQueryWrapper<>());
    }

    private void updateAllValue(Integer version,CoderRecommendConfig config){
        this.setCategoryTypeNums(config.getCategoryTypeNums());
        this.setSubstanceNums(config.getSubstanceNums());
        this.setThumbWeight(config.getThumbWeight());
        this.setFavWeight(config.getFavWeight());
        this.setUnlikeWeight(config.getUnlikeWeight());
        this.setHotRankNum(config.getHotRankNum());
        this.setHotSelectNum(config.getHotSelectNum());
        this.setHotRankDay(config.getHotRankDay());
        this.setHotThumbWeight(config.getHotThumbWeight());
        this.setHotFavWeight(config.getHotFavWeight());
        this.setPushCount(config.getPushCount());
        this.setVersion(version);
    }

    public Integer getCategoryTypeNums() {
        return categoryTypeNums;
    }

    public void setCategoryTypeNums(Integer categoryTypeNums) {
        this.categoryTypeNums = categoryTypeNums;
    }

    public Integer getSubstanceNums() {
        return substanceNums;
    }

    public void setSubstanceNums(Integer substanceNums) {
        this.substanceNums = substanceNums;
    }

    public Double getThumbWeight() {
        return thumbWeight;
    }

    public void setThumbWeight(Double thumbWeight) {
        this.thumbWeight = thumbWeight;
    }

    public Double getFavWeight() {
        return favWeight;
    }

    public void setFavWeight(Double favWeight) {
        this.favWeight = favWeight;
    }

    public Double getUnlikeWeight() {
        return unlikeWeight;
    }

    public void setUnlikeWeight(Double unlikeWeight) {
        this.unlikeWeight = unlikeWeight;
    }

    public Integer getHotRankNum() {
        return hotRankNum;
    }

    public void setHotRankNum(Integer hotRankNum) {
        this.hotRankNum = hotRankNum;
    }

    public Integer getHotSelectNum() {
        return hotSelectNum;
    }

    public void setHotSelectNum(Integer hotSelectNum) {
        this.hotSelectNum = hotSelectNum;
    }

    public Integer getHotRankDay() {
        return hotRankDay;
    }

    public void setHotRankDay(Integer hotRankDay) {
        this.hotRankDay = hotRankDay;
    }

    public Double getHotThumbWeight() {
        return hotThumbWeight;
    }

    public void setHotThumbWeight(Double hotThumbWeight) {
        this.hotThumbWeight = hotThumbWeight;
    }

    public Double getHotFavWeight() {
        return hotFavWeight;
    }

    public void setHotFavWeight(Double hotFavWeight) {
        this.hotFavWeight = hotFavWeight;
    }

    public Integer getPushCount() {
        return pushCount;
    }

    public void setPushCount(Integer pushCount) {
        this.pushCount = pushCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CoderRecommendConfig)) return false;
        CoderRecommendConfig config = (CoderRecommendConfig) o;
        return Objects.equals(getCategoryTypeNums(),
                config.getCategoryTypeNums()) &&
                Objects.equals(getSubstanceNums(),
                        config.getSubstanceNums()) &&
                Objects.equals(getThumbWeight(), config.getThumbWeight()) && Objects.equals(getFavWeight(), config.getFavWeight()) && Objects.equals(getUnlikeWeight(), config.getUnlikeWeight()) && Objects.equals(getHotRankNum(), config.getHotRankNum()) && Objects.equals(getHotSelectNum(), config.getHotSelectNum()) && Objects.equals(getHotRankDay(), config.getHotRankDay()) && Objects.equals(getHotThumbWeight(), config.getHotThumbWeight()) && Objects.equals(getHotFavWeight(), config.getHotFavWeight()) && Objects.equals(getPushCount(), config.getPushCount());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCategoryTypeNums(),
                getSubstanceNums(), getThumbWeight(),
                getFavWeight(), getUnlikeWeight(),
                getHotRankNum(), getHotSelectNum(),
                getHotRankDay(), getHotThumbWeight(),
                getHotFavWeight(), getPushCount());
    }
}
